package com.education.teach.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.di.module.SettingModule;
import com.education.teach.mvp.contract.SettingContract;
import com.education.teach.mvp.model.bean.LoginBean;
import com.education.teach.mvp.presenter.SettingPresenter;
import com.education.teach.utils.ARouterUtils;
import com.education.teach.utils.DnCommonUtils;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import dn.easy.common.base.AppManager;
import dn.easy.common.commonutils.SPUtils;
import dn.easy.common.commonutils.StringUtils;
import dn.easy.common.commonwidget.RippleView;
import dn.easy.common.constant.Contans;
import dn.easy.common.core.RouterHub;
import dn.easy.common.utils.SaveUserInfo;
import dn.easy.common.utils.bottompopmenu.BottomPopMenuString;
import static com.jess.arms.utils.Preconditions.checkNotNull;

public class SettingActivity extends BaseActivity<SettingPresenter> implements SettingContract.View {
    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.tv_username)
    TextView mTvUsername;
    @BindView(R.id.tv_update)
    TextView mTvUpdate;
    @BindView(R.id.versionCode)
    TextView mVersionCode;
    @BindView(R.id.tv_eqit_login)
    RippleView mTvEqitLogin;

    private BottomPopMenuString mBottomPopExit;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
//        DaggerSettingComponent //如找不到该类,请编译一下项目
//                .builder()
//                .appComponent(appComponent)
//                .settingModule(new SettingModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_setting; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mToolbar.setMainTitle("设置");
        mToolbar.setOnLeftTitleClickListener(view -> finish());
        initQuitLogin();
        LoginBean bean = DnCommonUtils.getUserModel(SettingActivity.this);
        if (bean != null){
            mTvUsername.setText("账号:  " + bean.getResult().getSfzh());
        }else{
            mTvUsername.setText("账号:  ");
        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private void initQuitLogin() {
        List<String> mPopList = new ArrayList<>();
        mPopList.add("确认");
        mPopList.add("取消");
        mBottomPopExit = new BottomPopMenuString(SettingActivity.this, mPopList,"确认切换账号吗？");
        mBottomPopExit.setOnButtonClickListener(position -> {
            switch (position){
                case 0 :
                    showMessage("退出登录成功！");
                    SaveUserInfo.logOut(SettingActivity.this,Contans.USERBEAN);
                    SPUtils.clearData(SettingActivity.this);
                    AppManager.getAppManager().finishAllActivity();
                    ARouterUtils.startAcitivty(this, LoginActivity.class);
                    finish();
                    break;

                default:
                    mBottomPopExit.diss();
                    break;
            }
        });
    }

    @OnClick({  R.id.tv_update, R.id.tv_eqit_login, R.id.tv_boot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_update:
                break;

            case R.id.tv_eqit_login:
                mBottomPopExit.show();
                break;

            case R.id.tv_boot:
                Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
                startActivity(intent);
                break;
        }
    }
}
